<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *项目管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$manage_op=array('start','off');
if(in_array($_RQ['op'],$manage_op)){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择模板！','error');
	}
}
if($_RQ['op']=='list'){
	$params=array();
	$params['uniacid']=$_SESSION['uniacid'];
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	if(isset($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$fields=array('id','uniacid','title','status','image','remark','paixu','createtime');
	$list=pdo_getslice('jy_aixiaoyi_mb',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['url']=custom_url('jy_aixiaoyi/tjform//id='.$item['id'],true);
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'模板列表'),
			array('op'=>'edit','title'=>'添加模板'),
		),
		'list'=>array('data'=>'list','tip'=>'[温馨提示]：问答优先匹配！','edit'=>true,'list'=>array(
			array('name'=>'title','title'=>'模板名称','type'=>'string'),
			array('name'=>'status','title'=>'是否启用','type'=>'switch','set'=>'set'),
			array('name'=>'url','title'=>'复制链接','type'=>'copy'),
			array('name'=>'url','title'=>'二维码','type'=>'ewm'),
		)),
		'search'=>array(
			array('name'=>'title','width'=>15,'title'=>'模板名称','type'=>'string'),
			array('name'=>'status','width'=>15,'title'=>'是否启用','type'=>'radio','options'=>'status'),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	$return=array();
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_aixiaoyi_mb',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$item=array();
	}
	$return['item']=$item;
	$return['item']['fileds']=json_decode($return['item']['fileds']);
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回项目列表'),
		),
		'edit_tab'=>true,
		'edits'=>array(
    						array(
    							'title'=>'基本信息',
    							'list'=>array(
    								array('type'=>'string','title'=>'模板名称','name'=>'title','help'=>''),
    								array('type'=>'string','title'=>'行业','name'=>'notice','help'=>''),
    								array('type'=>'image','title'=>'模板图片','name'=>'image','help'=>''),
    								array('type'=>'string','title'=>'排序','name'=>'paixu','help'=>'填写数字，按从小到大排序展示'),
    								array('type'=>'switch','title'=>'是否启用','name'=>'status','help'=>'设置是否启用'),
    								array('type'=>'text','title'=>'描述','name'=>'remark','help'=>''),
    								array('type'=>'text','title'=>'结尾补充内容','name'=>'jwcontent','help'=>'用于补全语句'),
    							)
    						),
    						array(
    							'title'=>'幻灯片',
        						'list'=>array(
									array('type'=>'array','title'=>'幻灯片','name'=>'hdps','add_tip'=>'添加一个幻灯片',
											'list'=>array(
												array('type'=>'string','name'=>'title','title'=>'标题'),
												array('type'=>'image','name'=>'picurl','title'=>'图片'),
												array('type'=>'string','name'=>'url','title'=>'地址'),
											),
											'details'=>array(
												array('type'=>'string','name'=>'title','title'=>'标题'),
												array('type'=>'image','name'=>'picurl','title'=>'图片','help'=>'推荐尺寸：750*360'),
												array('type'=>'link','name'=>'url','from'=>'action','title'=>'地址'),
											)
									),
    							)
    						),
							array(
							'title'=>'自定义字段',
							'list'=>array(
								array('type'=>'array','name'=>'fileds','add_tip'=>'添加一个字段（温馨提示：已经有用户提交过数据的表单，不允许再编辑和添加新字段！）',
								'list'=>array(
									array('type'=>'string','name'=>'title','title'=>'字段名称'),	
									array('type'=>'radio','name'=>'type','title'=>'字段类型','options'=>'types'),	
									array('type'=>'radio','name'=>'ismust','title'=>'必填','options'=>'ismust'),	
									array('type'=>'string','name'=>'paixu','title'=>'排序'),	
									array('type'=>'string','name'=>'value','title'=>'选项信息'),	
								),
								'details'=>array(
									array('type'=>'string','name'=>'title','title'=>'字段名称'),	
									array('type'=>'select','name'=>'type','title'=>'字段类型','options'=>'type','option_title'=>'type'),
									array('type'=>'switch','name'=>'ismust','title'=>'必填','help'=>''),
									array('type'=>'string','name'=>'paixu','title'=>'排序','help'=>'从小到大顺序排序'),
									array('type'=>'string','name'=>'value','title'=>'选项信息','help'=>'单选和多选设置此条信息，选项之间以英文分号;隔开，例如:选项1;选项2;选项3'),
								)
								),
							)
						),
			
			)
	);
	$return['type']=array(array('id'=>1,'type'=>'字符串'),array('id'=>2,'type'=>'数字'),array('id'=>4,'type'=>'单选'),array('id'=>5,'type'=>'多选'),array('id'=>6,'type'=>'文本'));
	$return['types']=array('1'=>'字符串','2'=>'数字','4'=>'单选框','5'=>'多选框','6'=>'文本');
	$return['ismust']=array('0'=>'否','1'=>'是');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata = get_postdata();
	$fileds=$postdata['fileds'];
	$data = array(
	    
		'title'=>trim($postdata['title']),
		'image'=>trim($postdata['image']),
		'remark'=>trim($postdata['remark']),
		'notice'=>trim($postdata['notice']), //行业
		'status'=>intval($postdata['status']),
		'paixu'=>intval($postdata['paixu']),
		'jwcontent'=>trim($postdata['jwcontent'])
	);
	$data['createtime'] = time();
	$id=intval($postdata['id']);
	if($id){
		$hasdata=pdo_getcolumn('jy_aixiaoyi_mb',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id),'fileds');
		$data['fileds']=ijson_encode($postdata['fileds']);
		pdo_update('jy_aixiaoyi_mb',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$data['fileds']=ijson_encode($postdata['fileds']);
		$data['uniacid']=$_SESSION['uniacid'];
		pdo_insert('jy_aixiaoyi_mb',$data);
		$serve_id=pdo_insertid();

	}
	exi('操作成功！','','list');
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_aixiaoyi_mb',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择要编辑的模板！','error');
	}
	$post=get_postdata();
	if(isset($post['status'])){
		pdo_update('jy_aixiaoyi_mb',array('status'=>intval($post['status'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	if(isset($post['isshow'])){
		pdo_update('jy_aixiaoyi_mb',array('isshow'=>intval($post['isshow'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	exi('操作成功！');
}
